package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    @Override
    public ResponseResult channels() {
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getStatus, 1);
        queryWrapper.orderByAsc(WmChannel::getOrd);
        List<WmChannel> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmChannelDto dto) {
        //分页查询
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询关键字
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), WmChannel::getName, dto.getName());
        //状态
        queryWrapper.eq(dto.getStatus() != null, WmChannel::getStatus, dto.getStatus());
        //创建时间倒序排序
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page(page, queryWrapper);
        //封装数据
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 保存频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //判断传入字段是否为空
        if (!StringUtils.isNoneEmpty(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断传入频道是否存在
        List<WmChannel> list = list();
        for (WmChannel channel : list) {
            //已存在,响应错误信息
            if (channel.getName().equals(wmChannel.getName())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.WM_CHANNEL_ALREADY_EXISTS);
            }
        }
        //不存在,补全其他字段
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        //保存
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //查询数据库,判断该频道是否已经禁用
        WmChannel wmChannel = getById(id);
        //如果没有禁用,不允许删除
        if (wmChannel.getStatus()) {
            //频道已启用
            return ResponseResult.errorResult(AppHttpCodeEnum.WM_CHANNEL_ENABLE);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        //判断是否要修改为禁用
        if (!wmChannel.getStatus()) {
            //查询自媒体文章表,判断是否被引用
            LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmNews::getChannelId, wmChannel.getId());
            int count = wmNewsService.count(queryWrapper);
            if (count > 0) {
                //频道已引用
                return ResponseResult.errorResult(AppHttpCodeEnum.WM_CHANNEL_USE);
            }
        }
        //查询出数据库里原来的数据
        WmChannel channel = getById(wmChannel.getId());
        //如果数据库中的频道名称和修改的频道名称不一致,说明修改了频道名称
        if (!channel.getName().equals(wmChannel.getName())) {
            //判读修改的频道名称是否已经存在
            LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmChannel::getName, wmChannel.getName());
            int count = count(queryWrapper);
            if (count > 0) {
                //频道已经存在,响应错误信息
                return ResponseResult.errorResult(AppHttpCodeEnum.WM_CHANNEL_ALREADY_EXISTS);
            }
        }
        //修改频道数据
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
